/* 
 *      Pila.java
 *
 *      Copyright 2008 Pablo Nicolas Diaz Bilotto <pablonicolas.diaz@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package i386CodeGenerator;

import java.util.Stack;


/**
 * The Class Pila.
 */
public class Pila {

	/** The cantint. */
	public int cantint = 0;

	/** The cant string. */
	public int cantString = 0;

	/** The cantdouble. */
	public int cantdouble = 0;

	/** The actcantint. */
	private int actcantint = 0;

	/** The actcantdouble. */
	private int actcantdouble = 0;

	/** The actcant string. */
	private int actcantString = 0;

	/** The elementos. */
	private Stack<String> elementos;

	/**
	 * Instantiates a new pila.
	 */
	public Pila() {
		elementos = new Stack<String>();
	}

	/**
	 * Empty.
	 * 
	 * @return true, if successful
	 */
	public boolean empty() {
		return (elementos.size() == 0);
	}

	/**
	 * Pop.
	 * 
	 * @return the string
	 */
	public String pop() {

		String a = elementos.pop();
		if (a.contains("double"))
			actcantdouble--;
		else if (a.contains("int"))
			actcantint--;
		else
			actcantString--;
		return a;
	}

	/**
	 * Push.
	 * 
	 * @param tipo the tipo
	 * 
	 * @return the string
	 */
	public String push(String tipo) {
		if ("int".equals(tipo)) {
			actcantint++;
			elementos.push("varAuxint" + actcantint);
			if (cantint <= actcantint)
				cantint = actcantint;
			return "varAuxUint" + actcantint;
		} else if ("double".equals(tipo)) {
			actcantdouble++;
			elementos.push("varAuxdouble" + actcantdouble);
			if (cantdouble <= actcantdouble)
				cantdouble = actcantdouble;
			return "varAuxFloat" + actcantdouble;
		} else {
			actcantString++;
			elementos.push("varAuxString" + actcantString);
			if (cantString <= actcantString)
				cantString = actcantString;
			return "varAuxString" + actcantString;
		}
	}

	/**
	 * Size.
	 * 
	 * @return the int
	 */
	public int size() {
		return elementos.size();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer listvar = new StringBuffer();
		for (int i = 0; i < this.cantdouble; i++)
			listvar.append("\tvarAuxdouble" + (i + 1) + " dq 0.0\n");
		for (int i = 0; i < this.cantint; i++)
			listvar.append("\tvarAuxint" + (i + 1) + " dw 0\n");
		for (int i = 0; i < this.cantString; i++)
			listvar.append("\tvarAuxString" + (i + 1) + " db ?\n");
		return listvar.toString();
	}
}
